package GUI;

import item.ShoppingCartItem;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import amsStore.Bill;

public class GUI_page03_Checkout extends JPanel {
	Bill bill;
	
	JLabel titleLabel;
	JButton returnButton;
	
	JList shoppingCart;
	JScrollPane shoppingCartScroller;
	DefaultListModel listModel;
	
	JLabel creditCardNumberLabel;
	JTextField creditCardNumberField;
	
	JLabel creditCardExpiryLabel;
	JTextField creditCardExpiryField;
	
	JLabel orderAmountLabel;
	JButton confirmButton;
	
	public GUI_page03_Checkout() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setBackground(Color.white);
		// title label
		titleLabel = new JLabel("Shopping Checkout", JLabel.CENTER);
		titleLabel.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabel.setHorizontalTextPosition(JLabel.CENTER);
		titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 20));
		titleLabel.setForeground(Color.gray);
        this.add(titleLabel);
        
        this.add(Box.createRigidArea(new Dimension(0,20)));

        // return button
        returnButton = GUI_AMSStoreMain.makeGrayWhiteButton("Cancel", Component.LEFT_ALIGNMENT);
        this.add(returnButton);
        
        this.add(Box.createRigidArea(new Dimension(0,10)));
        
        // shopping cart list
        listModel = new DefaultListModel();
		listModel.addElement("Empty");
		shoppingCart = new JList(listModel);
		shoppingCart.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		shoppingCart.setLayoutOrientation(JList.VERTICAL);
		shoppingCart.setVisibleRowCount(-1);
		shoppingCart.setSelectedIndex(0);
		shoppingCartScroller = new JScrollPane(shoppingCart);
		shoppingCartScroller.setPreferredSize(new Dimension(250, 250));
		this.add(shoppingCartScroller);
		
		 this.add(Box.createRigidArea(new Dimension(0,10)));
        
        // Credit Card number field
        creditCardNumberLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Credit Card Number", 11);
		this.add(creditCardNumberLabel);
		creditCardNumberField = GUI_AMSStoreMain.makeLeftGrayTextField("", 90);
		this.add(creditCardNumberField);
		
		this.add(Box.createRigidArea(new Dimension(0,5)));
		
		 // Credit Card expiry field
		creditCardExpiryLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Credit Card Expiry (yyyy-mm-dd)", 11);
		this.add(creditCardExpiryLabel);
		creditCardExpiryField = GUI_AMSStoreMain.makeLeftGrayTextField("yyyy-mm-dd", 90);
		this.add(creditCardExpiryField);
		
		this.add(Box.createRigidArea(new Dimension(0,10)));
		
		 // order amount label
        orderAmountLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel("Order Total: $00.00", 14);
		this.add(orderAmountLabel);
		
		this.add(Box.createRigidArea(new Dimension(0,5)));
		
		// confirm button
        confirmButton = GUI_AMSStoreMain.makeGrayWhiteButton("Confirm", Component.LEFT_ALIGNMENT);
        this.add(confirmButton);
	}
	
	public void resetPage(Bill theBill)
	{
		this.creditCardNumberField.setText("");
		this.creditCardExpiryField.setText("yyyy-mm-dd");
		
		bill = theBill;
		this.remove(shoppingCartScroller);
		
		listModel = new DefaultListModel();
		for (int i = 0; i < bill.getBillItems().size(); i++)
		{
			ShoppingCartItem tempItem = bill.getBillItems().get(i);
			listModel.addElement(tempItem.getQuantity() + " " + tempItem.getTitle() + " @" + tempItem.getSellPrice());
		}
		shoppingCart = new JList(listModel);
		shoppingCart.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		shoppingCart.setLayoutOrientation(JList.VERTICAL);
		shoppingCart.setVisibleRowCount(-1);
		shoppingCart.setSelectedIndex(0);
		shoppingCartScroller = new JScrollPane(shoppingCart);
		shoppingCartScroller.setPreferredSize(new Dimension(250, 250));
		this.add(shoppingCartScroller,4);
		Float amount = bill.getTotalPurchaseAmount();
		orderAmountLabel.setText("Order Total: $" + amount.toString());
	}
}
